suppressPackageStartupMessages({
  library(ggplot2)
  library(readr)
  library(ggthemes)
  library(dplyr)
})

## theming for plots
wft <- theme_tufte(16)
wft_rotxlab <- theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))

# generate a density of the distribution of best alphafold pLDDT values
# from the pLDDT summary
# expected input columns: best_pLDDT
plot_alphafold_plddt_summary <- function(x) {
    # bin the plddt
    x$conflvl <- cut(x$best_pLDDT, breaks = c(0, 50, 70, 90, 100),
                     right = TRUE,
                     labels = c("<50", "50-70", "70-90", ">90"))

    labels <- x %>%
      group_by(conflvl) %>%
      summarize(n = n())
    labels$label <- sprintf("%3i / %3i", labels$n, nrow(x))
    labels$pos <- c(40, 60, 80, 95)

    p <- ggplot(x, aes(y = best_pLDDT)) +
      geom_density(size = 1) +
      geom_hline(yintercept = c(50, 70, 90), linetype = 3) +
      geom_text(aes(y = pos, label = label, x = 0.045), data = labels) +
      scale_y_continuous(breaks = c(50, 70, 90)) +
      labs(x = "Density", y = "Best mean pLDDT of each OPG") +
      wft

    return(p)
}
